package Model;

import java.util.*;

/*
 * Person model AddressBook, holds list of Persons. Implemented as singleton since we don't want to have
 * more address books to work with.
 */

public class AddressBook
{
    // local fields
    private ArrayList<Person> addressBook;
    private static AddressBook instance = null;
    
    // private constructor
    private AddressBook()
    {
       addressBook = new ArrayList<Person>();
    }
    
    // implementation of singleton
    public static AddressBook getInstance()
    {
        if (instance == null)
        {
            instance = new AddressBook();
        }
        return instance;
    }
    
    // adding a Person to the address book
    public void insertPerson(Person person)
    {
        addressBook.add(person);
    }
    
    // enables us to work with AddressBook in controller
    public ArrayList<Person> getAddressBook()
    {
        return addressBook;
    }
    
    // returns single person from AddressBook
    public Person getPerson(int personId)
    {
        Person foundPerson = null;
        for(Person person : addressBook)
        {
            if(person.getId() == personId){
                foundPerson = person;
            }
        }
        return foundPerson;
    }
}
